package com.chenyi.leetcode.editor.cn;

import java.util.Stack;

/**
 * [20]有效的括号
 *
 * @author by chenyi
 * @className ValidParentheses
 * @date 2022-04-05 22:39:00
 */
public class ValidParentheses {
    public static void main(String[] args) {
        Solution solution = new ValidParentheses().new Solution();
        System.out.println(solution.isValid("[()]"));
    }

    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
            //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isValid(String s) {
            char[] chars = s.toCharArray();
            Stack<Character> stack = new Stack<>();
            for (int i = 0; i < chars.length; i++) {
                char brackets = chars[i];
                if (brackets == '{' || brackets == '[' || brackets == '(') {
                    stack.push(brackets);
                } else {
                    if (!stack.isEmpty()) {
                        if (brackets == ')' && stack.pop() != '(') {
                            return false;
                        } else if (brackets == ']' && stack.pop() != '[') {
                            return false;
                        } else if (brackets == '}' && stack.pop() != '{') {
                            return false;
                        }
                    } else {
                        return false;
                    }
                }
            }
            return stack.isEmpty();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
